package org.honeynet.hbbackend.ipfilter;

import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.ejb.Startup;

import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;

@Singleton
@Startup
public class Cache {
	
	private final static int SIZE = 2000;
	
	// like Collections.newSetFromMap()
	private final ConcurrentLinkedHashMap<String, Boolean> cache;
	
	public Cache() {
		cache = new ConcurrentLinkedHashMap.Builder<String, Boolean>()
	    .maximumWeightedCapacity(SIZE)
	    .build();
	}
	
	@Lock(LockType.READ)
	public boolean contains(String ip) {
		return cache.containsKey(ip);
	}
	
	@Lock(LockType.WRITE)
	public void add(String ip) {
		cache.put(ip, false);
	}
	

}
